package adminServlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import adminejb.AircraftBeanLocal;
import adminejb.AirlineBeanLocal;
import adminejb.AirportBeanLocal;
import adminejb.FlightBeanLocal;
import repositories.FlightRepository;
import specifications.FlightSpecification;
import entities.Aircraft;
import entities.Airline;
import entities.Airport;
import entities.Flight;

import java.util.Map;

/**
 * Servlet implementation class editFlight
 */
@WebServlet("/getParamsToEdit")
public class getParamsToEdit extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public getParamsToEdit() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    @EJB
    FlightBeanLocal flightBean;
    
    @EJB
    AirlineBeanLocal airlineBean;
    
    @EJB
    AirportBeanLocal airportBean;
    
    @EJB
    AircraftBeanLocal aircraftBean;
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	response.setContentType("text/html;charset=UTF-8");
    	Flight foundFlight = flightBean.findFlight("flightNo", new String(request.getParameter("id").getBytes("ISO-8859-1"),"UTF-8"));
    	
    	Map<String, String> map = new HashMap<String, String>();
    	map.put("action", "edit");
    	map.put("id", String.valueOf(foundFlight.getId()));
    	map.put("flightNo", request.getParameter("id"));
    	map.put("flightairline", foundFlight.getFlightAirline().getAirlineName());
    	map.put("flightaircraft", foundFlight.getFlightAircraft().getAircraftType());
    	map.put("depairport", foundFlight.getDepartureAirport().getAirportName());
    	map.put("arrairport", foundFlight.getArriveAirport().getAirportName());
    	map.put("price_ec", foundFlight.getPriceEC());
    	map.put("price_bc", foundFlight.getPriceBC());
    	map.put("price_fc", foundFlight.getPriceFC());
    	map.put("av_ec", foundFlight.getAvEC());
    	map.put("av_bc", foundFlight.getAvBC());
    	map.put("av_fc", foundFlight.getAvFC());
    	map.put("deptime", foundFlight.getDepartureTime());
    	map.put("arrtime", foundFlight.getArriveTime());
    	
    	ArrayList<Airline> airlineList = airlineBean.getAllAirlines();
    	ArrayList<Aircraft> aircraftList = aircraftBean.getAllAircrafts();
    	ArrayList<Airport> airportList = airportBean.getAllAirports();  
    	
    	request.setAttribute("airlineList", airlineList);    	
    	request.setAttribute("aircraftList", aircraftList);    	
    	request.setAttribute("airportList", airportList); 
    	
    	//request.setAttribute("arrairport", foundFlight.getArriveAirport().getAirportName());
    	
    	request.setAttribute("map", map);
    	getServletContext().getRequestDispatcher("/admin/EditFlight.jsp").forward(request, response);
    	
    }
   
    
    
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);	
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);	
	}

}
